<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 控件数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\StageService;
use think\Request;

class StageController extends VerifyController
{
    /**
     * @var StageService
     */
    private StageService $stageService;

    public function __construct()
    {
        parent::__construct();
        $this->stageService = StageService::getInstance();
    }

    /**
     * 添加阶段
     * @param Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        try {
            $res = $this->stageService->save($request->param()['data']);
            return json(success_response('', $res));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新阶段
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        try {
            $res = $this->stageService->save($request->param()['data']);
            return json(success_response('', $res));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除阶段 批量
     * @param Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $res = $this->stageService->remove($request->param()['param']);
        return json(success_response('', $res));
    }

    /**
     * 模板列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function list(Request $request)
    {
        $res = $this->stageService->list($request->param()['param'] ?? []);
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getOne(Request $request)
    {
        $id = $request->param()['param']['filter']['id'] ?? 0;
        $res = $this->stageService->getOne($id);
        return json(success_response('', $res));
    }

    /**
     * 阶段排序
     * @param Request $request
     * @return \think\Response
     */
    public function getStageOrderList(Request $request)
    {
        $res = $this->stageService->getStageOrderList($request->param()['param']['filter'] ?? [], $request->getTenantId());
        return json(success_response('', $res));
    }

}
